package org.robot.scm.spider;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.robot.scm.utils.JsonHelper;
import org.robot.scm.utils.RequestHelper;
import org.robot.scm.utils.StreamUtils;

/**
 * @Author : Awn create on 2024/4/24 16:32
 */
public class VideoSpider {

    public static void main(String[] args) {
        List<String> allImages = Lists.newArrayList();
        for (int i = 1; i <= 100; i++) {
            System.out.println("当前处理: " + i + "页");
            read(allImages, i);
        }
        System.out.println(JsonHelper.toJSONString(allImages));
    }

    private static void read(List<String> allImages, int i) {
        try {
            String result = RequestHelper.get("http://www.sharenice.net/t-576O5aWz?page=" + i);
            List<String> htmlUrls = readHrefRegSrc(result);
            List<String> filters = StreamUtils.filter(htmlUrls, e -> e.contains("/douyin/") || e.contains("/kuaishou/") || e.contains("/weishi/") || e.contains("/huoshan/"));
            Set<String> distinct = Sets.newHashSet(filters);
            distinct.forEach(e -> {
                String detailResult = RequestHelper.get(e);
                allImages.addAll(readVideoRegSrc(detailResult));
            });
        } catch (Exception e) {
            System.out.println("read error, page : " + i + ", error: " + e.getMessage());
        }
    }

    private static List<String> readHrefRegSrc(String artHtml) {
        List<String> result = Lists.newArrayList();
        String regex = "<a[^>]+href=\"([^\"]+)\"";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(artHtml);
        while (matcher.find()) {
            String srcUrl = matcher.group(1);
            if (StringUtils.isNotBlank(srcUrl)) {
                result.add(srcUrl);
            }
        }
        return result;
    }

    private static List<String> readVideoRegSrc(String artHtml) {
        List<String> result = Lists.newArrayList();
        String regex = "<video[^>]+src=\"([^\"]+)\"";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(artHtml);
        while (matcher.find()) {
            String srcUrl = matcher.group(1);
            if (StringUtils.isNotBlank(srcUrl)) {
                result.add(srcUrl);
            }
        }
        return result;
    }

}
